<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
    <el-form-item label="股票名称" prop="stockSymbol">
      <el-input
          v-model="queryParams.stockSymbol"
          placeholder="请输入股票名称"
          clearable
          @keyup.enter="handleQuery"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
    </el-form-item>
  </el-form>


    <el-form :inline="true" label-width="68px">
      <el-form-item label="生成股票" prop="stockQuantity">
        <el-input
            v-model="stockQuantity"
            placeholder="请输入股票数量"
            clearable
            @keyup.enter="initializeStockMessage"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="initializeStockMessage">生成</el-button>
      </el-form-item>
    </el-form>


    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="isDisabled"
            @click="handleDelete(null)"
        v-hasPermi="['system:stock:remove']"
        >删除</el-button>
      </el-col>


      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stockList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="股票名称" align="center" prop="stockSymbol" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:stock:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改股票信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="stockRef" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Stock">
import { listStock, delStock,initializeStockMessageStock } from "@/api/admin/stocks";
import { ref, reactive, toRefs, getCurrentInstance } from 'vue'
import {ElMessageBox} from "element-plus";
import toast from "@/utils/toast";
const { proxy } = getCurrentInstance();

const stockList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const total = ref(0);
const title = ref("");
const stockQuantity=ref(null);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    stockData: null,
    stockSymbol: null,
    stockQuantity: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询股票信息列表 */
function getList() {
  loading.value = true;
  listStock(queryParams.value).then(response => {
    stockList.value = response.data.map(symbol => ({
      stockSymbol: symbol  // 确保选中时拿得到 stockSymbol
    }));
    total.value = response.total;
    loading.value = false;
  });
}


// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    stockSymbol: null,
    stockData: null,
    lastUpdated: null
  };
  proxy.resetForm("stockRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

//生成股票按钮
function initializeStockMessage(){
  console.log(stockQuantity.value)
  initializeStockMessageStock(stockQuantity.value);
  getList();
}

// 多选框选中数据
const isDisabled = ref(true);



function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.stockSymbol);
  single.value = selection.length === 1;
  isDisabled.value = selection.length === 0;
}




function handleDelete(row) {
  let stockSymbols = [];

  if (row) {
    // 单个删除，直接使用该行的 stockSymbol
    stockSymbols = [row.stockSymbol];
  } else {
    // 批量删除，检查是否有选中项
    if (ids.value.length === 0) {
      toast.warning("请先勾选要删除的股票！");
      return;
    }
    stockSymbols = ids.value;  // 批量删除时使用选中的所有股票
  }

  // 确认删除操作
  ElMessageBox.confirm(
      '是否确认删除股票信息为 "' + stockSymbols.join(', ') + '" 的数据项？',
      '删除股票',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }
  ).then(() => {
    delStock(stockSymbols).then(() => {
      getList();
      toast.success("删除成功");
    }).catch(() => {});
  }).catch(() => {
  });
}



getList();
</script>
